using System;
using System.Collections.Generic;
using System.Data;

namespace AutoTool
{
    public class StoragePlansRepository
    {
        private readonly DatabaseHelper _dbHelper;

        public StoragePlansRepository(string connectionString)
        {
            _dbHelper = new DatabaseHelper(connectionString);
        }

        public List<StoragePlans> GetAllStoragePlans()
        {
            string query = "SELECT * FROM StoragePlans";
            DataTable result = _dbHelper.ExecuteQuery(query);
            return DataMapper.MapToList<StoragePlans>(result);
        }

        public StoragePlans GetStoragePlansById(Guid id)
        {
            string query = "SELECT * FROM StoragePlans WHERE PlanId = @PlanId";
            var parameters = new Dictionary<string, object>
            {
                { "@PlanId", id }
            };

            DataTable result = _dbHelper.ExecuteQuery(query, parameters);
            if (result.Rows.Count > 0)
            {
                return DataMapper.MapToObject<StoragePlans>(result.Rows[0]);
            }
            return null;
        }

        public void InsertStoragePlans(StoragePlans entity)
        {
            string query = "INSERT INTO StoragePlans (PlanId, PlanName, PlanDescription, TotalSpace, Price, Duration) VALUES (@PlanId, @PlanName, @PlanDescription, @TotalSpace, @Price, @Duration)";
            var parameters = new Dictionary<string, object>
            {
                { "@PlanId", entity.PlanId },
                { "@PlanName", entity.PlanName },
                { "@PlanDescription", entity.PlanDescription },
                { "@TotalSpace", entity.TotalSpace },
                { "@Price", entity.Price },
                { "@Duration", entity.Duration }
            };

            _dbHelper.ExecuteNonQuery(query, parameters);
        }

        public void DeleteStoragePlans(Guid id)
        {
            string query = "DELETE FROM StoragePlans WHERE PlanId = @PlanId";
            var parameters = new Dictionary<string, object>
            {
                { "@PlanId", id }
            };

            _dbHelper.ExecuteNonQuery(query, parameters);
        }
    }
}